import { Toast } from "vant";
import cTitle from "components/title";
export default {
  components: {
    cTitle
  },
  data() {
    return {
      orderId: null,
      storeName: "",
      uploadUrl: "upload.uploadPic", //上传图片
      fileList: [],
      evaluateType: 3,
      type: 1, //判断是哪个页面跳转
      comment: "",
      images: [],
      recommendList: [
        {
          image: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/consumption-coupons/recommend.png",
          onImage: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/consumption-coupons/recommend-on.png",
          name: "推荐",
          type: 3
        },
        {
          image: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/consumption-coupons/ordinary.png",
          onImage: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/consumption-coupons/ordinary-on.png",
          name: "一般",
          type: 2
        },
        {
          image: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/consumption-coupons/not-recommend.png",
          onImage: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/consumption-coupons/not-recommend-on.png",
          name: "不推荐",
          type: 1
        }
      ]
    };
  },
  activated() {
    this.uploadUrl = this.fun.getRealUrl("upload.uploadPic", {});
    if ((this.$route.query.orderId ?? "") !== "") {
      this.orderId = Number(this.$route.query.orderId);
      this.type = Number(this.$route.query.type);
      this.storeName = this.$route.query.storeName;
    }
  },
  methods: {
    initData() {
      this.orderId = null;
      this.evaluateType = 1;
      this.comment = "";
      this.images = [];
    },
    evaluateText(event) {
      this.setData({
        comment: event.detail.value
      });
    },
    onRecommend(type) {
      this.evaluateType = type;
    },
    multipleMethod(e, flag) {
      if (e && e.length) {
        for (let i = 0; i < e.length; i++) {
          this.onRead_1(e[i], flag);
        }
      } else {
        this.onRead_1(e, flag);
      }
    },
    // vant 多图片上传
    onRead_1(e, flag) {
      Toast.loading({
        message: "上传中",
        forbidClick: true
      });
      // this.stopButton = true;
      let fd = new FormData();
      if (flag === true) {
        fd.append("file", e);
      } else {
        fd.append("file", e.file); // 第一个参数字符串可以填任意命名，第二个根据对象属性来找到file
      }
      var That = this;
      axios
        .post(this.uploadUrl, fd, {
          headers: { "Content-Type": "multipart/form-data" }
        })
        .then(response => {
          var responseData = response.data;
          if (responseData.result === 1) {
            Toast.clear();
            That.images.push(responseData.data.img_url);
          } else {
            Toast(responseData.msg);
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    removeImg_1(delIndex) {
      this.images.splice(delIndex, 1);
    },
    evaluateForm() {
      let json = {
        order_id: this.orderId,
        comment_type: this.evaluateType,
        comment: this.comment,
        images: this.images
      };
      $http.post("plugin.coupon-store.frontend.comment.comment", json, "正在获取").then(
        response => {
          if (response.result == 1) {
            Toast(response.msg);
            this.initData();
            setTimeout(res => {
              this.$router.go(-1);
            }, 1000);
          } else {
            this.$dialog.alert({ message: response.msg });
          }
        },
        function(response) {
          console.log(response);
        }
      );
    }
  }
};
